var express = require('express')
var router = express.Router()

// 导入用户集合的字段的数据模型、导入生成token的模块
var bannerModel = require('../model/goods/BannerModel')
var jwt = require('../utils/jwt')


router.get('/list', (req, res) => {
  jwt.verifyToken(req, res).then(() => {
    bannerModel.find().then(data => {
      res.json({ err: 0, msg: 'success', data })
    })
  })
})

router.post('/add', (req, res) => {
  jwt.verifyToken(req, res).then(() => {
    let { img_name, img_url, status, target_url } = req.body
    const data = {
      img_name,
      img_url,
      status,
      target_url
    }
    bannerModel.insertMany([data]).then(data => {
      res.json({ err: 0, msg: 'success', data: {data} })
    })
  })
})

router.get('/del', (req, res) => {
  jwt.verifyToken(req, res).then(() => {
    let { _id } = req.query
    bannerModel.deleteOne({_id}).then(data => {
      res.json({ err: 0, msg: 'success', data: {data} })
    })
  })
})

router.get('/status', (req, res) => {
  jwt.verifyToken(req, res).then(() => {
    let { _id, status } = req.query
    bannerModel.updateOne({ _id }, { $set: { status } }).then(data => {
      res.json({ err: 0, msg: 'success', data: {data} })
    })
  })
})

module.exports = router